package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.domain.Result;
import com.dongdong.em.ResultEnum;
import com.dongdong.pojo.PageBean;
import com.dongdong.pojo.TbSpecifType;
import com.dongdong.pojo.TbSpecification;
import com.dongdong.pojo.TbSpecificationOption;
import com.dongdong.service.TbSpecificationService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
@RequestMapping("tbSpecification")
public class TbSpecificationController {
   @Reference(timeout = 50000)
   private TbSpecificationService tbSpecificationService;
    @RequestMapping("getInfo")
    public String getInfo(){
        return "/admin/specification";
    }
    @RequestMapping("getShow")
    @ResponseBody
    public Result getShow(String specName, @RequestParam(defaultValue = "1",required = false)Integer pageNumber, @RequestParam(defaultValue = "3",required = false)Integer pageSize){
        PageBean<TbSpecification> pageBean = tbSpecificationService.getShow(specName,pageNumber,pageSize);
        return  Result.buildResult(ResultEnum.SUCCESS).setData(pageBean);
    }
    @RequestMapping("save")
    @ResponseBody
    public Result save(String optionListJson,TbSpecification tbSpecification){
      List<TbSpecificationOption>  list = JSONObject.parseArray(optionListJson,TbSpecificationOption.class);
        Boolean b =     tbSpecificationService.save(list,tbSpecification);
        return  Result.buildResult(ResultEnum.SUCCESS).setData(b);
    }
    @RequestMapping("update")
    @ResponseBody
    public Result update(Long id){
        List<TbSpecifType> list =  tbSpecificationService.update(id);
        return  Result.buildResult(ResultEnum.SUCCESS).setData(list);
    }
    @RequestMapping("getShowTo")
    @ResponseBody
    public Result getShowTo(){
       List<TbSpecification>list = tbSpecificationService.getShowTo();
        return  Result.buildResult(ResultEnum.SUCCESS).setData(list);
    }



    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbSpecification tbSpecification,String tbSpecificationJson){
        List<TbSpecificationOption>  list = JSONObject.parseArray(tbSpecificationJson,TbSpecificationOption.class);
        tbSpecificationService.updateInfo(tbSpecification,list);
    return Result.buildResult(ResultEnum.SUCCESS);
    }

    @RequestMapping("deleteAll")
    @ResponseBody
    public Result  deleteAll(@RequestParam("ids[]")Long[] ids){
        Boolean  b=  tbSpecificationService.deleteAll(ids);
        return  Result.buildResult(ResultEnum.SUCCESS).setData(b);
    }

}
